<template>
	<div>
		<el-row :gutter="10">
			<el-col :span="24">
				<el-card shadow="never" class="card">
					<div id="charts_one" style="width:100%;min-height:80vh">
            <Search size='small' :searchVisible="searchVisible" :searchData.sync="searchData"  :searchForm="searchForm" @refresh_list="index"></Search>
						<div class="btn-group" style="margin-bottom:11px;">
               <div>
                 <el-button size="small" v-loading="materialLoading" @click="reloadMaterial? getMaterialReloadResult(true): doMaterialSync()" :icon="reloadMaterial? 'el-icon-loading': 'el-icon-refresh'">{{ reloadMaterial? '同步中，点击刷新': '同步物料' }}</el-button>
<!--							   <el-button type="success"  size="mini" icon="el-icon-plus"  @click="add(ids)" v-if="checkPermission('/app/Material/add.html')" >{{$t('action.add')}}</el-button>   -->
<!--                 <el-button type="primary" :disabled="single" size="mini" icon="el-icon-edit"  @click="update(ids)" v-if="checkPermission('/app/Material/update.html')" >{{$t('action.update')}}</el-button>-->
<!--							   <el-button type="danger" :disabled="multiple" size="mini" icon="el-icon-delete"  @click="del(ids)" v-if="checkPermission('/app/Material/delete.html')" >{{$t('action.delete')}}</el-button>     -->
<!--                 <el-button type="info" :disabled="single" size="mini" icon="el-icon-view"  @click="detail(ids)" v-if="checkPermission('/app/Material/detail.html')" >{{$t('action.detail')}}</el-button>-->
               </div>
                 <table-tool :searchVisible.sync="searchVisible"  :fieldList="fieldList" @refresh_list="index" :searchTool="true"></table-tool>
						</div>
					    <el-table :row-class-name="rowClass" @selection-change="selection"  @row-click="handleRowClick"  row-key="id"  :header-cell-style="{ background: '#eef1f6', color: '#606266' }" v-loading="loading"  ref="multipleTable" border class="eltable" :data="list"  style="width: 100%">		   
						    <el-table-column align="center" type="selection" width="42"></el-table-column>
<!--							<el-table-column align="center" type='' v-if="fieldList[0].checked" property="id" label="ID" show-overflow-tooltip width="70">-->
<!--							</el-table-column>-->
<!--							<el-table-column align="center"  v-if="fieldList[1].checked" property="fmaterialid" :label="$t('field.Material_FMATERIALID')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
                <el-table-column align="center" type="index" label="No." width="50" >
                </el-table-column>
							<el-table-column align="center"  v-if="fieldList[2].checked" property="fnumber" :label="$t('field.Material_FNUMBER')" show-overflow-tooltip width="" >
							</el-table-column>
                <el-table-column align="center"  v-if="fieldList[21].checked" property="fname" :label="$t('field.Material_FName')" show-overflow-tooltip width="" >
                </el-table-column>
<!--							<el-table-column align="center"  v-if="fieldList[3].checked" property="foldnumber" :label="$t('field.Material_FOLDNUMBER')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
							<el-table-column align="center"  v-if="fieldList[4].checked" property="fspecification" :label="$t('field.Material_FSPECIFICATION')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[5].checked" property="fmaterialgroup" :label="$t('field.Material_FMATERIALGROUP')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[6].checked" property="fcreateorg" :label="$t('field.Material_FCREATEORG')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[7].checked" property="fuseorg" :label="$t('field.Material_FUSEORG')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[8].checked" property="fcreatedate" :label="$t('field.Material_FCREATEDATE')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[9].checked" property="fmodifier" :label="$t('field.Material_FMODIFIER')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[10].checked" property="fmodifydate" :label="$t('field.Material_FMODIFYDATE')" show-overflow-tooltip width="" >
							</el-table-column>
<!--							<el-table-column align="center"  v-if="fieldList[11].checked" property="fstatus" :label="$t('field.Material_FSTATUS')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
							<el-table-column align="center"  v-if="fieldList[12].checked" property="fforbidstatus" :label="$t('field.Material_FFORBIDSTATUS')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[13].checked" property="fapprover" :label="$t('field.Material_FAPPROVER')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[14].checked" property="fapprovedate" :label="$t('field.Material_FAPPROVEDATE')" show-overflow-tooltip width="" >
							</el-table-column>
<!--							<el-table-column align="center"  v-if="fieldList[15].checked" property="fforbidder" :label="$t('field.Material_FFORBIDDER')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
<!--							<el-table-column align="center"  v-if="fieldList[16].checked" property="fforbiddate" :label="$t('field.Material_FFORBIDDATE')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
<!--							<el-table-column align="center"  v-if="fieldList[17].checked" property="fcounttype" :label="$t('field.Material_FCountType')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
							<el-table-column align="center"  v-if="fieldList[18].checked" property="fqualityinstock" :label="$t('field.Material_FQualityInstock')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[19].checked" property="fcoefficient" :label="$t('field.Material_FCoefficient')" show-overflow-tooltip width="" >
							</el-table-column>
							<el-table-column align="center"  v-if="fieldList[20].checked" property="fcreateusername" :label="$t('field.Material_FCREATEUSERNAME')" show-overflow-tooltip width="" >
							</el-table-column>
                <el-table-column align="center"  v-if="fieldList[22].checked" property="stockName" :label="$t('field.Material_stockName')" show-overflow-tooltip width="" >
                </el-table-column>
<!--							<el-table-column align="center"  v-if="fieldList[21].checked" property="fname" :label="$t('field.Material_FName')" show-overflow-tooltip width="" >-->
<!--							</el-table-column>-->
<!--							  <el-table-column :fixed="$store.getters.device !== 'mobile'?'right':false" :label="$t('system.operation')" align="center" width="160">-->
<!--								<template slot-scope="scope">-->
<!--									<div v-if="scope.row.id">-->
<!--&lt;!&ndash;										<el-button size="mini" type="primary" @click="update(scope.row)"  v-if="checkPermission('/app/Material/update.html')"><i class="el-icon-edit" />{{$t('action.update')}}</el-button>&ndash;&gt;-->
<!--										<el-button size="mini" type="danger" @click="del(scope.row)"  v-if="checkPermission('/app/Material/delete.html')"><i class="el-icon-delete" />{{$t('action.delete')}}</el-button>-->
<!--									</div>-->
<!--								</template>-->
<!--							  </el-table-column>-->
						</el-table>
						<Page :total="page_data.total" :page.sync="page_data.page" :limit.sync="page_data.limit" @pagination="index" />
					</div>
				</el-card>
			</el-col>
		</el-row>
	    <!--添加-->
<!--		<Add  :show.sync="dialog.addDialogStatus" size="small" @refresh_list="index"></Add>-->
		<!--修改-->
<!--		<Update :info="updateInfo" :show.sync="dialog.updateDialogStatus" size="small" @refresh_list="index"></Update>-->
		<!--查看详情-->
		<Detail :info="detailInfo" :show.sync="dialog.detailDialogStatus" size="small" @refresh_list="index"></Detail>
	</div>
</template>
<script>
	import {
	    index, 
	    updateExt, 
	    getUpdateInfo,
	    del,
    getSyncReload,
      sync,
	} from '@/api/app/material'
	import Search from '@/components/common/Search.vue'
	import Page from '@/components/common/Page.vue'
	import TableTool from '@/components/common/TableTool.vue'
	import { confirm,param2Obj } from '@/utils/common'
	import Add from '@/views/app/material/add.vue' 
	import Update from '@/views/app/material/update.vue' 
	import Detail from '@/views/app/material/detail.vue' 
	export default {
		name:'material',
		components: {
			Search,
			Page,
			TableTool,
	        Add,  
	        Update,  
	        Detail,  
		},
		data(){
			return {
				dialog: {
	                    addDialogStatus : false,           
	                    updateDialogStatus : false,           
	                    detailDialogStatus : false,           
				},
				fieldList:[
					{lable:'ID',checked:true},
				    {lable:this.$t('field.Material_FMATERIALID'),checked:true},
				    {lable:this.$t('field.Material_FNUMBER'),checked:true},
				    {lable:this.$t('field.Material_FOLDNUMBER'),checked:true},
				    {lable:this.$t('field.Material_FSPECIFICATION'),checked:true},
				    {lable:this.$t('field.Material_FMATERIALGROUP'),checked:true},
				    {lable:this.$t('field.Material_FCREATEORG'),checked:true},
				    {lable:this.$t('field.Material_FUSEORG'),checked:true},
				    {lable:this.$t('field.Material_FCREATEDATE'),checked:true},
				    {lable:this.$t('field.Material_FMODIFIER'),checked:true},
				    {lable:this.$t('field.Material_FMODIFYDATE'),checked:true},
				    {lable:this.$t('field.Material_FSTATUS'),checked:true},
				    {lable:this.$t('field.Material_FFORBIDSTATUS'),checked:true},
				    {lable:this.$t('field.Material_FAPPROVER'),checked:true},
				    {lable:this.$t('field.Material_FAPPROVEDATE'),checked:true},
				    {lable:this.$t('field.Material_FFORBIDDER'),checked:true},
				    {lable:this.$t('field.Material_FFORBIDDATE'),checked:true},
				    {lable:this.$t('field.Material_FCountType'),checked:true},
				    {lable:this.$t('field.Material_FQualityInstock'),checked:true},
				    {lable:this.$t('field.Material_FCoefficient'),checked:true},
				    {lable:this.$t('field.Material_FCREATEUSERNAME'),checked:true},
				    {lable:this.$t('field.Material_FName'),checked:true},
				    {lable:this.$t('field.Material_stockName'),checked:true},
				],
				ids: [],
				selectRows:[],
				selectTotal:0,
				single:true,
				multiple:true,
				list: [],
				small_tables:{},
	            addInfo:{},
	            updateInfo:{},
	            detailInfo:{},
				loading: false,
				page_data: {
					limit: 20,
					page: 1,
					total:20,
				},
				searchVisible:true,
				searchForm:[],
				searchData:{},
        materialLoading: false,
        reloadMaterial: false,
			}
		},
		methods: {
      /**
       * 获取加载按钮的加载中状态
       */
       getMaterialReloadResult(showMsg) {
        this.materialLoading = true
        try {
          getSyncReload({type:"物料"}).then(res =>{
            // if (!res.res) return
              console.log(res)

            if (res.data.reloadOrder) {
              this.$message.info('物料数据同步中')
            } else {
              if (res.data.reloadRes) {
                this.$message.success('已同步完成, 请刷新查看最新结果')
              } else {
                this.$message.error('同步失败，' + this.reloadMsg)
              }
            }

          })

        } finally {
          this.materialLoading = false
        }
      },
      /**
       * 同步物料
       * */
      async doMaterialSync() {
        try {
          let msg = '同步物料，耗时较长，是否继续执行？';
          await this.$confirm(msg, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            dangerouslyUseHTMLString: true
          })
        } catch(e) {
          return
        }
        try {
          this.reloadMaterial = true
         sync().then(res => {
           if (!res.res) return
           this.$message.success('已经开始同步')
         })

        } finally {
          this.loading = false
        }
      },
			index(){
				let param = {limit:this.page_data.limit,page:this.page_data.page}
				Object.assign(param, this.searchData,param2Obj(this.$route.fullPath))
				this.loading = true
				index(param).then(res => {
					this.list = res.data.data
					this.small_tables=res.sql_field_data
					this.page_data.total = res.data.total
					this.loading = false
					if(this.page_data.page == 1){
						this.searchForm = [
              {type:'Input',label:this.$t('field.Material_FNUMBER'),prop:'fnumber',width:'150px'},
              {type:'Input',label:this.$t('field.Material_FName'),prop:'fname',width:'150px'},
            ]
					}
				})
			},
			updateExt(row,field){
				if(row.id){
					updateExt({id:row.id,[field]:row[field]}).then(res => {
						this.$message({message: this.$t('system.success'), type: 'success'})
					})
				}
			},
			add(){
				this.dialog.addDialogStatus = true
			},
			update(row){
				let id = row.id ? row.id : this.ids.join(',')
				getUpdateInfo({id:id}).then(res => {
					if(res.status == 200){
						this.dialog.updateDialogStatus = true
						this.updateInfo = res.data
					}
				})
			},
			del(row){
				confirm({content:this.$t('system.confirmOperation')}).then(() => {
					let ids = row.id ? row.id : this.ids.join(',')
					del({id:ids}).then(res => {
						this.$message({message: this.$t(res.msg), type: 'success'})
						this.index()
					})
				}).catch(() => {})
			},
			detail(row){
				this.dialog.detailDialogStatus = true
				this.detailInfo = {id:row.id ? row.id : this.ids.join(',')}
			},
			selection(selection) {
			    this.selectRows=selection
			    this.selectTotal=selection.length
				this.ids = selection.map(item => item.id)
				this.single = selection.length != 1
				this.multiple = !selection.length
			},
			handleRowClick(row, rowIndex,event){
				if(event.target.className !== 'el-input__inner'){
					this.$refs.multipleTable.toggleRowSelection(row)
				}
			},
			rowClass ({ row, rowIndex }) {
				for(let i=0;i<this.ids.length;i++) {
					if (row.id === this.ids[i]) {
						return 'rowLight'
					}
				}
			},
      syncMaterial(){
        sync(res => {
          if(!res.res)return
        })


      },
		},	
		mounted(){
			this.index()
		}
	}
</script>
<style  lang="scss">
@import '@/assets/scss/common.scss';
</style>